﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Shellfacemillingcutter2
    {
        public static void _Shellfacemillingcutter2(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);


            double scale = 1; //放大系数

            double D = Convert.ToDouble(data["dc"]) * scale;
            double d1 = Convert.ToDouble(data["dcon"]) * scale;
            double d2 = Convert.ToDouble(data["dccb"]) * scale;
            double d3 = Convert.ToDouble(data["d3"]) * scale;
            double H = Convert.ToDouble(data["oal"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;
            double a = Convert.ToDouble(data["a"]) * scale;
            double kr = Convert.ToDouble(data["kapr"]) * scale;

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X, v1.Y - H - 6);
            Vector2 v3 = new Vector2(v1.X + a / 2, v1.Y - 5);
            Vector2 v4 = new Vector2(v3.X, v3.Y - a / 4.578);
            Vector2 v5 = new Vector2(v3.X + 1, v3.Y + 2);
            Vector2 v6 = new Vector2(v1.X + d3 / 2 - 1, v5.Y);
            Vector2 v7 = new Vector2(v6.X + 1, v6.Y - 1.5);
            Vector2 v34 = new Vector2(v1.X + D / 2, v6.Y - H);
            Vector2 v35 = fun.Polar(v34, kr * pi / 180, H / 3.375);
            Vector2 v8 = new Vector2(v35.X - 4, v7.Y);
            Vector2 v9 = new Vector2(v8.X + 2, v8.Y - 1.5);
            Vector2 v10 = new Vector2(v35.X - 2, v35.Y + 1);
            Vector2 v11 = new Vector2(v34.X - 2, v34.Y + 1);
            Vector2 v12 = new Vector2(v11.X * -1, v11.Y);
            Vector2 v13 = new Vector2(v10.X * -1, v10.Y);
            Vector2 v14 = new Vector2(v9.X * -1, v9.Y);
            Vector2 v15 = new Vector2(v8.X * -1, v8.Y);
            Vector2 v16 = new Vector2(v7.X * -1, v7.Y);
            Vector2 v17 = new Vector2(v6.X * -1, v6.Y);
            Vector2 v18 = new Vector2(v5.X * -1, v5.Y);
            Vector2 v19 = new Vector2(v3.X * -1, v3.Y);
            Vector2 v20 = new Vector2(v4.X * -1, v4.Y);
            Vector2 v21 = new Vector2(v1.X + d1 / 2, v3.Y);
            Vector2 v42 = new Vector2(v21.X + 2.7, v5.Y);
            Vector2 v22 = new Vector2(v42.X * -1, v42.Y);
            Vector2 v23 = new Vector2(v21.X * -1, v21.Y);
            Vector2 v24 = new Vector2(v23.X, v5.Y - l);
            Vector2 v25 = new Vector2(v21.X, v24.Y);
            Vector2 v26 = new Vector2(v1.X + d2 / 2 - 1.5, v25.Y);
            Vector2 v27 = new Vector2(v26.X + 1.5, v26.Y - 1.5);
            Vector2 v28 = new Vector2(v27.X, v11.Y + H / 8.64);
            Vector2 v29 = new Vector2(v26.X * -1, v26.Y);
            Vector2 v30 = new Vector2(v27.X * -1, v27.Y);
            Vector2 v31 = new Vector2(v28.X * -1, v28.Y);
            Vector2 v32 = new Vector2(v28.X + 3, v28.Y);
            Vector2 v33 = new Vector2(v31.X - 3, v31.Y);
            Vector2 v36 = new Vector2(v34.X - 3, v34.Y);
            Vector2 v37 = new Vector2(v36.X - 2, v11.Y);
            Vector2 v38 = new Vector2(v34.X * -1, v34.Y);
            Vector2 v39 = new Vector2(v35.X * -1, v35.Y);
            Vector2 v40 = new Vector2(v36.X * -1, v36.Y);
            Vector2 v41 = new Vector2(v37.X * -1, v37.Y);


            //作图
            //1
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v33));
            line2.Vertexes.Add(new LwPolylineVertex(v32));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v3));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v20));
            line2.Vertexes.Add(new LwPolylineVertex(v19));
            line2.Vertexes.Add(new LwPolylineVertex(v18));
            line2.Vertexes.Add(new LwPolylineVertex(v22));
            line2.Vertexes.Add(new LwPolylineVertex(v17));
            line2.Vertexes.Add(new LwPolylineVertex(v16));
            line2.Vertexes.Add(new LwPolylineVertex(v15));
            line2.Vertexes.Add(new LwPolylineVertex(v14));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            fun.PolyFill(dxf, line2, 0.3, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v22));
            line3.Vertexes.Add(new LwPolylineVertex(v23));
            line3.Vertexes.Add(new LwPolylineVertex(v24));
            line3.Vertexes.Add(new LwPolylineVertex(v25));
            line3.Vertexes.Add(new LwPolylineVertex(v21));
            line3.Vertexes.Add(new LwPolylineVertex(v42));
            line3.Vertexes.Add(new LwPolylineVertex(v21));
            line3.Vertexes.Add(new LwPolylineVertex(v3));
            fun.PolyFill(dxf, line3, 0.3, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v31));
            line4.Vertexes.Add(new LwPolylineVertex(v30));
            line4.Vertexes.Add(new LwPolylineVertex(v29));
            line4.Vertexes.Add(new LwPolylineVertex(v26));
            line4.Vertexes.Add(new LwPolylineVertex(v27));
            line4.Vertexes.Add(new LwPolylineVertex(v28));
            line4.Vertexes[1].Bulge = Math.Tan(-90 * pi / 180 / 4);
            line4.Vertexes[3].Bulge = Math.Tan(-90 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.3, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v23));
            line5.Vertexes.Add(new LwPolylineVertex(v19));
            fun.PolyFill(dxf, line5, 0.3, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v35));
            line6.Vertexes.Add(new LwPolylineVertex(v34));
            line6.Vertexes.Add(new LwPolylineVertex(v36));
            line6.Vertexes.Add(new LwPolylineVertex(v37));
            fun.PolyFill(dxf, line6, 0.3, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v39));
            line7.Vertexes.Add(new LwPolylineVertex(v38));
            line7.Vertexes.Add(new LwPolylineVertex(v40));
            line7.Vertexes.Add(new LwPolylineVertex(v41));
            fun.PolyFill(dxf, line7, 0.3, contour);


            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v27));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v32));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v11));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v10));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v9));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v8));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v7));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v6));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v42));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v21));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly1.Vertexes[0].Bulge = Math.Tan(-90 * pi / 180 / 4);
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);

            LwPolyline hatchpoly2 = new LwPolyline();
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v29));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v30));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v31));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v33));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v12));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v14));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v15));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v16));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v17));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v22));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v23));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v24));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v29));
            hatchpoly2.Vertexes[0].Bulge = Math.Tan(90 * pi / 180 / 4);
            hatchpoly2.IsClosed = true;
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            Arc[] harc2 = new Arc[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst2; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
                if (htype2[i] == "Arc")
                {
                    harc2[i] = (Arc)hatchpoly2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype2, hnumst2, 45, 10, hline2, harc2, hatching);




            //标注

            double height = 2;
            double arrowsize = 1;
            double exline = 2;




            fun.dimrot1(dxf, v23, v21, 0, d1 / 2, 1, height, arrowsize, exline, "d1=" + d1, "", "");

            fun.dimrot1(dxf, v16, v7, 0, d1, 1, height, arrowsize, exline, "d3=" + d3, "", "");

            fun.dimrot1(dxf, v14 , v9, 0, d1 * 3 / 2, 1, height, arrowsize, exline, "D1", "", "");

            fun.dimrot1(dxf, v6, v34, -90, d1, 1, height, arrowsize, exline, "H=" + H, "", "");

            fun.dimrot1(dxf, v28, v31, 180, d1 / 2, 1, height, arrowsize, exline, "d2=" + d2, "", "");

            fun.dimrot1(dxf, v38, v34, 180, d1, 1, height, arrowsize, exline, "D" + D, "", "");

            fun.dimrot1(dxf, v17, v29, 90, d1 * 2, 1, height, arrowsize, exline, "l=" + l, "", "");

            fun.jdbz1(dxf, new Line(v34, fun.Polar(v34, 0 * pi / 180, 0.1)), new Line(v34, fun.Polar(v34, kr * pi / 180, 0.1)), 15, "kr", 1);

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }



            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}
